using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerGroundState : PlayerState
{
    public PlayerGroundState(Player player, PlayerMoveData playerData, StateMachine stateMachine, string animBoolName) : base(player, playerData, stateMachine, animBoolName)
    {
        stateType = StateType.Ground;
    }

    public override void Enter()
    {
        base.Enter();

        if (player.stateMachine.LastState != null && player.stateMachine.LastState.stateType != StateType.Ground  && player.stateMachine.LastState != player.DashState)
        {
            player.DashState.RefreshCoolTime();
        }
        
        player.JumpState.ResetJumpCount();
    }

    public override void LogicUpdate()
    {
        base.LogicUpdate();

        if (!isTouchingWall && !isTouchingCeiling && scrollInput && isGrounded)
        {
            stateMachine.ChangeState(player.ScrollState);
        }
        else if (dashInput && !isTouchingWall && !isTouchingCeiling && player.DashState.CanDash())
        {
            player.SetVelocityZero();
            if (xInput != 0 && yInput != -1 || yInput == 1)
            {
                player.DashState.SetDashDirection(new Vector2Int(xInput, yInput));
            }
            else
            {
                player.DashState.SetDashDirection(new Vector2Int(player.FacingDirection, 0));
            }
            stateMachine.ChangeState(player.DashState);
        }
        else if (isTouchingWall && !isTouchingCeiling && grabInput)
        {
            stateMachine.ChangeState(player.WallGrabState);
        }
        else if (jumpInput && player.JumpState.CanJump() && !isTouchingCeiling)
        {
            stateMachine.ChangeState(player.JumpState);
        }
        else if (!isGrounded)
        {
            player.InAirState.SetCanJumpDelay();
            stateMachine.ChangeState(player.InAirState);
        }
    }

    public override void OnCheck()
    {
        base.OnCheck();

        isLeftFootGrounded = player.CheckLeftFootIfGrounded();
        isRightFootGrounded = player.CheckRightFootIfGrounded();
        isGrounded = isLeftFootGrounded || isRightFootGrounded;
        isSingleFootGrounded = isLeftFootGrounded && !isRightFootGrounded || !isLeftFootGrounded && isRightFootGrounded;
        isTouchingCeiling = player.CheckIsTouchingCeiling();
        isTouchingWall = player.CheckIsTouchingWall();
    }
}
